layui.use(['table','layer','form','laydate'], function(){
	var table = layui.table,
	layer = layui.layer,
	$ = layui.$,
	laydate = layui.laydate,
	form = layui.form;
	
	/*@时间*/
	/*开始时间*/
	laydate.render({
	  	elem: '#start' //指定元素
	});
	/*结束时间*/
	laydate.render({
	  	elem: '#end' //指定元素
	});
	/*@配件返库初始化table*/
	table.render({
	    elem: '#table-list',
	    url: '/repair/stock/index', //数据接口
	    id: 'table_list',
	    page: true, //开启分页
	    cols: [[ //表头
	    	{field: 'id', title: '序号', type: 'checkbox'},
	    	{field: 'goods_name', title: '产品名称'},
	    	{field: 'repair_sn', title: '相关维修订单编号'},
	    	{field: 'number', title: '数量'},
	    	{field: 'unit', title: '单位'},
	    	{field: 'selling_price', title: '零售单价（元）'},
	    	{field: 'trade_price', title: '批发价（元）'},
	    	{field: 'buying_price', title: '进货价（元）'},
	    	{field: 'position_text', title: '原所在位置'},
	    	{title: '操作', toolbar: '#table-btn'},
	    ]],
	    /*渲染前，请求成功后回调*/
	    parseData: function(res){
	    	if(res.code === 0){
	    		var num = (res.page-1) * res.row
		    	for(var i = 0;i < res.data.length;++i){
		    		res.data[i].id = num + i + 1
		    	}
	    	}
	    }
	});
	/*@配件返库历史初始化table*/
	var tableObj = table.render({
	    elem: '#log-table-list',
	    url: '/repair/stock/log_list', //数据接口
	    id: 'log_table_list',
	    page: true, //开启分页
	    cols: [[ //表头
	    	{field: 'id', title: '序号', type: 'checkbox'},
	    	{field: 'ret_time', title: '返库时间'},
	    	{field: 'goods_name', title: '产品名称'},
	    	{field: 'repair_sn', title: '相关维修订单编号'},
	    	{field: 'number', title: '数量'},
	    	{field: 'unit', title: '单位'},
	    	{field: 'selling_price', title: '零售单价（元）'},
	    	{field: 'trade_price', title: '批发价（元）'},
	    	{field: 'buying_price', title: '进货价（元）'},
	    	{field: 'position_text', title: '原所在位置'},
	    ]],
	    /*渲染前，请求成功后回调*/
	    parseData: function(res){
	    	if(res.code === 0){
	    		var num = (res.page-1) * res.row
		    	for(var i = 0;i < res.data.length;++i){
		    		res.data[i].id = num + i + 1
		    	}
		    	exportData = res.data
	    	}
	    }
	});
	/*@表单查询*/
	form.on('submit(search)',function(data){
		var obj = data.elem;
		var field = data.field;
		console.log(111111);
		// if(field.start != '' && field.end != ''){
		// 	var startDate = new Date(field.start).getTime();
		// 	var endDate = new Date(field.end).getTime();
		// 	if(startDate > endDate){
		// 		layer.msg('开始时间不能大于结束时间',{icon: 2})
		// 		return false
		// 	}
		// }
		// console.log(field)
		// var ty = $(obj).attr('ty')
		// var page = {};
		// if(ty == 'search'){
		// 	page.page = 1;
		// }else{
		// 	page.page = 1;
		// 	page.limit = 'limit';
		// }
		// /*重载表格数据*/
		// tableObj.reload({
		// 	page,
  //           where: field
  //       });
  //       if(ty == 'export'){
  //       	layer.confirm('确定导出', {icon: 3, title:'提示'}, function(index){
  //               layer.close(index);
  //               table.exportFile(tableObj.config.id,exportData, 'xls');
  //           })
  //       }
  //       return false
	});
	/*@导出*/
	form.on('submit(search)',function(data){

	})
	/*@配件返库*/
	return_house = function(param, number){
		layer.confirm('确定是否将'+number+'个维修配件返回库存', {icon: 3, title:'提示'} ,function(){
  			$.post('/repair/stock/return_house', param, function(msg){
  				console.log(msg)
  				if(msg.code === 1){
  					layer.msg(msg.msg, {icon: 6, time: 1000}, function(){
  						table.reload('table_list')
  					})
  				}else{
  					layer.msg(msg.msg, {icon: 5, time: 2000})
  				}
  			})
  		})
	}
	/*@监听工具*/
	table.on('tool(list)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj.data; //获得当前行数据
	  	var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj.tr; //获得当前行 tr 的DOM对象

	  	if(layEvent == 'query_ret'){ //确认返库
	  		var param = {};
  			param.ids = [data.stock_repair_id]
  			param.goods_id = [data.goods_id]
  			param.number = [data.number]
  			return_house(param, data.number)
	  	}
	});
	/*@批量返库*/
	batch_return = function(){
		var data = table.checkStatus('table_list').data
		var param = {};
		param.ids = []
		param.goods_id = []
		param.number = []
		var number = 0;
		for(var i = 0;i < data.length; ++i){
			param.ids.push(data[i].stock_repair_id)
			param.goods_id.push(data[i].goods_id)
			param.number.push(data[i].number)
			number = number + Number(data[i].number)
		}
		return_house(param, number)
	}
	/*@select选择监听*/
	form.on('select(select)',function(data){
		console.log(data)
		if(data.value == ''){
			$('select[name="brand_name"]').html('')
			$('select[name="memory_name"]').html('')
			$('select[name="attribute_name"]').html('')
			$('select[name="colour_name"]').html('')
			form.render("select");
			return
		}
		$.post('/repair/stock/get_attribute',{fid: data.value},function(msg){
			if(msg.code === 1){
				for(var k in msg.data){
					var html = '<option value="">全部</option>';
					for(var i = 0;i < msg.data[k].length;++i){
						html += '<option value="'+msg.data[k][i].attribute_name+'">'+msg.data[k][i].attribute_name+'</option>';
					}
					$('select[name="'+k+'"]').html(html)
				}
				form.render("select");
			}else{
				layer.msg(msg.msg, {icon: 5, time: 2000})
			}
		})
	})

})